package com.liuqi.aiagent.controller;

import com.liuqi.aiagent.service.ChatRecordService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.dto.AgentChatRecordDeleteDto;
import com.liuqi.aiagent.transfer.dto.AgentChatRecordFindDto;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.vo.AgentChatRecordVo;
import com.liuqi.aiagent.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuqi
 * @date 2025/7/22
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/chat/record")
@Slf4j
@Api(tags = "聊天记录服务接口")
public class ChatRecordController {

    private final ChatRecordService chatRecordService;

    @ApiOperation("查找用户在Agent上的聊天记录")
    @PostMapping("/findAgentRecord")
    public Result<List<AgentChatRecordVo>> findAgentRecord(@RequestBody AgentChatRecordFindDto dto) {
        if (StringUtil.notHasText(dto.getAgentName())) {
            return ApiResult.fail("agent 名称为空");
        }
        if (StringUtil.notHasText(dto.getUsername())) {
            return ApiResult.fail("用户名称为空");
        }
        if (dto.getLimit() != null && dto.getLimit() < 1) {
            return ApiResult.fail("查询最近消息记录条数不能小于 1");
        }
        if (dto.getLimit() == null) {
            dto.setLimit(10);
        }

        return chatRecordService.findRecentlyOfVo(
                dto.getAgentName(),
                dto.getUsername(),
                dto.getLimit()
        );
    }

    @ApiOperation("删除用户在Agent上的聊天记录")
    @PostMapping("/delete")
    public Result delete(@RequestBody AgentChatRecordDeleteDto dto) {
        if (StringUtil.notHasText(dto.getAgentName())) {
            return ApiResult.fail("agent 名称为空");
        }
        if (StringUtil.notHasText(dto.getUsername())) {
            return ApiResult.fail("用户名称为空");
        }

        return chatRecordService.deleteByAgentNameAndUsername(dto.getAgentName(), dto.getUsername());
    }

}
